# -*- coding: utf-8 -*-
# @Author : y
# @Date   : 2021/6/15
# @File   : EG36_EntProvidentFundCheck.py
# @E-mail : yantao@bbdservice.com

'''eof
name:申请企业是否缴存公积金
code:EG36_EntProvidentFundCheck
tableName:
columnName:
groups:企业通用模块
dependencies:FGW_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''
import sys
import pandas as pd
import json
from datetime import datetime
import time


def format_data(tableName):
    try:
        eg = FGW_DSJ.get("data").get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]

def get_date_list():
    try:
        now = datetime.now()
        aaa = [(now + pd.tseries.offsets.DateOffset(months=i)).strftime('%Y%m%d') for i in [-36, 0]]
        return aaa
    except:
        return None

def get_data_one(main_element,branch_element,null_list):
    try:
        element_list = format_data(main_element)
        if element_list == [{}]:
            return None
        elif len(element_list)>=1:
            value=element_list[0].get(branch_element)
            return None if value in null_list else value
        return None
    except:
        return None

def get_result():
    '''
    申请企业是否在公积金缴存名单中
    Factor= IF 申请企业 == 住房公积金缴存信息（zfgjjInfo）中单位名称（corpname） THEN 1 ELSE 0
    '''
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        zfgjj_info_list = format_data("zfgjjInfo")
        #申请企业
        basic_info = get_data_one("stClApplicatBasicInfoD","entName",null_type_list)
        if zfgjj_info_list == [{}] or basic_info in null_type_list :
            return u'缺失值'
        else:
            if len(zfgjj_info_list) >= 1:
                factor = []
                for i in zfgjj_info_list:
                    corp_name = i.get("corpname")
                    if corp_name not in null_type_list:
                        if basic_info==corp_name:
                            return 1
                        else:
                            factor.append(0)
                return 0 if len(factor) > 0 else u'缺失值'
            else:
                return 0
    except:
        return u'缺失值'


if __name__ == "__main__":
    # file_obj = open(r'../data/DAS/Past3YearPatentedInventionsNum/FGW_DSJ.json', encoding='utf-8', mode='r')
    file_obj = open(r'./FGW_DSJ.json', encoding='utf-8', mode='r')
    content = file_obj.read()
    FGW_DSJ = json.loads(content, strict=False)
    aa = get_result()
    print(aa)

result = get_result()
